package com.enzoism.springboot.dive.controller;

import com.enzoism.springboot.dive.model.User;
import com.enzoism.springboot.dive.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 新增用户
     */
    @PostMapping
    public ResponseEntity<User> addUser(@RequestBody User user) {
        try {
            User savedUser = userService.addUser(user);
            return ResponseEntity.ok(savedUser);
        } catch (Exception e) {
            log.error("新增用户失败：{}", e.getMessage());
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    /**
     * 根据复合key查询用户
     */
    @GetMapping("/{userId}/{roleId}")
    public ResponseEntity<User> getUserByCompositeKey(
            @PathVariable Long userId, 
            @PathVariable Long roleId) {
        User user = userService.getUserByCompositeKey(userId, roleId);
        return user != null ? ResponseEntity.ok(user) : ResponseEntity.notFound().build();
    }
    
    /**
     * 查询所有用户
     */
    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }
    
    /**
     * 根据userId查询用户列表
     */
    @GetMapping("/userId/{userId}")
    public ResponseEntity<List<User>> getUsersByUserId(@PathVariable Long userId) {
        List<User> users = userService.getUsersByUserId(userId);
        return ResponseEntity.ok(users);
    }
    
    /**
     * 根据roleId查询用户列表
     */
    @GetMapping("/roleId/{roleId}")
    public ResponseEntity<List<User>> getUsersByRoleId(@PathVariable Long roleId) {
        List<User> users = userService.getUsersByRoleId(roleId);
        return ResponseEntity.ok(users);
    }
    
    /**
     * 更新用户
     */
    @PutMapping
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        try {
            User updatedUser = userService.updateUser(user);
            return ResponseEntity.ok(updatedUser);
        } catch (Exception e) {
            log.error("更新用户失败：{}", e.getMessage());
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{userId}/{roleId}")
    public ResponseEntity<Void> deleteUser(
            @PathVariable Long userId, 
            @PathVariable Long roleId) {
        boolean success = userService.deleteUser(userId, roleId);
        return success ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }
    
    /**
     * 判断用户是否存在
     */
    @GetMapping("/exists/{userId}/{roleId}")
    public ResponseEntity<Boolean> existsUser(
            @PathVariable Long userId, 
            @PathVariable Long roleId) {
        boolean exists = userService.existsUser(userId, roleId);
        return ResponseEntity.ok(exists);
    }
    
    /**
     * 手动清除指定用户缓存
     */
    @DeleteMapping("/cache/{userId}/{roleId}")
    public ResponseEntity<Void> evictUserCache(
            @PathVariable Long userId, 
            @PathVariable Long roleId) {
        userService.evictUserCache(userId, roleId);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 清除所有用户缓存
     */
    @DeleteMapping("/cache/all")
    public ResponseEntity<Void> evictAllUsersCache() {
        userService.evictAllUsersCache();
        return ResponseEntity.ok().build();
    }
}